package com.mollytask.institution;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mollytask.R;
import com.mollytask.base.CheckAdapter;
import com.mollytask.bean.DataCheckList;
import com.mollytask.bean.DataFill;
import com.mollytask.bean.Institution;
import com.mollytask.bean.MyUser;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by freshxu on 2017/5/23.
 */

public class ReviewResultFragment extends Fragment {

    private RecyclerView mRecyclerView;

    private List<DataCheckList> mDataChecks = new ArrayList<>();

    private List<DataFill> mDataFills = new ArrayList<>();

    private CheckAdapter mAdapter;

    private MyUser myUser;

    private String name;

    private Institution mInstitution;

    public static ReviewResultFragment newInstance() {
        ReviewResultFragment myFragment = new ReviewResultFragment();
        return myFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.framgent_datareview, container, false);
        initRecyclerView(view);
        myUser = BmobUser.getCurrentUser(MyUser.class);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initRecyclerView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.dataRecycler);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, false);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.check_btn:
                        Intent intent = new Intent(getActivity(), ReviewResultActivity.class);
                        intent.putExtra("dataFillResult", mDataFills.get(position));
                        intent.putExtra("institution", mInstitution);
                        startActivity(intent);
                        break;
                }
            }
        });


    }


    private void initData() {
        mDataChecks.clear();
        mDataFills.clear();

        //根据机构编号查询到机构的名字
        BmobQuery<Institution> query = new BmobQuery<>();
        query.addWhereEqualTo("mInCode", myUser.getCode());
        query.findObjects(new FindListener<Institution>() {
            @Override
            public void done(List<Institution> list, BmobException e) {
                if (e == null) {
                    mInstitution = list.get(0);
                    name = mInstitution.getmInName();
                    //根据机构编号查询到数据库中自己填报的记录
                    BmobQuery<DataFill> bmobQuery = new BmobQuery<>();
                    bmobQuery.addWhereEqualTo("mInId", mInstitution.getmInCode());
                    query.order("-createdAt");
                    bmobQuery.findObjects(new FindListener<DataFill>() {
                        @Override
                        public void done(List<DataFill> list, BmobException e) {
                            if (e == null) {
                                for (int i = 0; i < list.size(); i++) {
                                    DataFill dataFill = list.get(i);
                                    DataCheckList dataCheckList = new DataCheckList();
                                    dataCheckList.setName(name);
                                    String fillId = dataFill.getmFillId();
                                    String data = fillId.substring(9, 13) + "年第" + fillId.substring(13, 14) + "季度";
                                    dataCheckList.setData(dataFill.getmFillId());
                                    dataCheckList.setData(data);
                                    dataCheckList.setStatus(dataFill.getStatus().toString());
                                    //添加至集合中
                                    mDataChecks.add(dataCheckList);
                                    mDataFills.add(dataFill);
                                }
                                if (mAdapter == null) {
                                    mAdapter = new CheckAdapter(R.layout.item_check, mDataChecks, false);
                                    mRecyclerView.setAdapter(mAdapter);
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    });
                }
            }
        });


    }
}
